/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.onegene.pdf.component.support;

import com.itextpdf.io.font.otf.GlyphLine;

import com.itextpdf.layout.splitting.ISplitCharacters;
import com.itextpdf.layout.splitting.DefaultSplitCharacters;

/**
 *
 * @author Bluesky
 */
public class ChineseSplitCharacters implements ISplitCharacters {

    // a line of text cannot start with any following characters in NOT_BEGIN_CHARACTERS[]
    static final char[] NOT_BEGIN_CHARACTERS = new char[]{
        '\u00b7', // ·
        '\u2013', // –
        '\u2014', // —
        '\u2019', // ’
        '\u201d', // ”
        '\u2026', // …
        '\u3001', // 、
        '\u3002', // 。
        '\u3009', // 〉
        '\u300b', // 》
        '\u300d', // 」
        '\u300f', // 』
        '\u3011', // 】
        '\u3015', // 〕
        '\uff01', // ！
        '\uff09', // ）
        '\uff0c', // ，
        '\uff0e', // ．
        '\uff1a', // ：
        '\uff1b', // ；
        '\uff1f', // ？
        '\uff5e' // ～
    };

    // a line of text cannot end with any following characters in NOT_ENDING_CHARACTERS[]
    static final char[] NOT_ENDING_CHARACTERS = new char[]{
        '\u2018', // ‘
        '\u201c', // “
        '\u3008', // 〈
        '\u300a', // 《
        '\u300c', // 「
        '\u300e', // 『
        '\u3010', // 【
        '\u3014', // 〔
        '\uff08' // （
    };

    /**
     * An instance DefaultSplitCharacter used for BasicLatin characters.
     */
    private static final ISplitCharacters defaultSplitCharacters = new DefaultSplitCharacters();

    @Override
    public boolean isSplitCharacter(GlyphLine text, int glyphPos) {

        if (!text.get(glyphPos).hasValidUnicode()) {
            return false;
        }

        int next = glyphPos + 1;
        if (next < text.end) {
            int charNext = text.get(next).getUnicode();
            for (char not_begin_character : NOT_BEGIN_CHARACTERS) {
                if (charNext == not_begin_character) {
                    return false;
                }
            }
        }

        int charCurr = text.get(glyphPos).getUnicode();
        for (char not_ending_character : NOT_ENDING_CHARACTERS) {
            if (charCurr == not_ending_character) {
                return false;
            }
        }

        return defaultSplitCharacters.isSplitCharacter(text, glyphPos);
    }
}
